#!/usr/bin/env bash

source ./config/env.sh
clear

mkdir -p ${K8S_DATA}/config/docker

cat > ${K8S_DATA}/config/docker/docker.service <<EOF
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.io

[Service]
WorkingDirectory=${K8S_DATA}/data/docker
Environment="PATH=${K8S_BIN_DIR}:/bin:/sbin:/usr/bin:/usr/sbin"
EnvironmentFile=-/run/flannel/docker
ExecStart=${K8S_BIN_DIR}/dockerd --log-level=error \$DOCKER_NETWORK_OPTIONS
ExecReload=/bin/kill -s HUP \$MAINPID
Restart=on-failure
RestartSec=5
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Delegate=yes
KillMode=process

[Install]
WantedBy=multi-user.target
EOF


cat > ${K8S_DATA}/config/docker/docker-daemon.json <<EOF
{
    "registry-mirrors": ["https://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn"],
    "insecure-registries": ["192.168.1.119","192.168.1.156"],
    "max-concurrent-downloads": 20,
    "live-restore": true,
    "max-concurrent-uploads": 10,
    "debug": true,
    "data-root": "${K8S_DATA}/data/docker/data",
    "exec-root": "${K8S_DATA}/data/docker/exec",
    "log-opts": {
      "max-size": "100m",
      "max-file": "5"
    }
}
EOF

cd ${K8S_DATA}/config/docker

for master_ip in ${MASTER_IP}
do
    echo "=========================================== ${master_ip} ==========================================="
    scp ${K8S_DATA}/config/docker/docker.service root@${master_ip}:/etc/systemd/system/
    ssh root@${master_ip} "mkdir -p ${K8S_DATA}/data/docker/{data,exec}"
    ssh root@${master_ip} "test -e /etc/docker || mkdir -p /etc/docker"
    scp ${K8S_DATA}/config/docker/docker-daemon.json root@${master_ip}:/etc/docker/daemon.json
done

for master_ip in ${MASTER_IP}
do
    echo "=========================================== ${master_ip} ==========================================="
    ssh root@${master_ip} "systemctl stop firewalld && systemctl disable firewalld"
    ssh root@${master_ip} "/usr/sbin/iptables -F && /usr/sbin/iptables -X && /usr/sbin/iptables -F -t nat && /usr/sbin/iptables -X -t nat"
    ssh root@${master_ip} "/usr/sbin/iptables -P FORWARD ACCEPT"
    ssh root@${master_ip} "systemctl daemon-reload && systemctl enable docker && systemctl restart docker"
    ssh root@${master_ip} "sudo sysctl -p /etc/sysctl.d/kubernetes.conf"
done


for master_ip in ${MASTER_IP}
do
    echo "=========================================== ${master_ip} ==========================================="
    ssh root@${master_ip} "systemctl status docker|grep Active"
done


for master_ip in ${MASTER_IP}
do
    echo "=========================================== ${master_ip} ==========================================="
    ssh root@${master_ip} "/usr/sbin/ip addr show flannel1.1 && /usr/sbin/ip addr show docker0"
done

